<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js misc - ubiquity - test2</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background-color: #000;
				margin: 0px;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

		<script src="../build/three.js"></script>

		<script src="js/renderers/Projector.js"></script>
		<script src="js/renderers/CanvasRenderer.js"></script>

		<script src="js/libs/stats.min.js"></script>

		<div id='container1' style='float: left '></div>
		<div id='container2' style='float: right'></div>

		<script>

			var stats;

			var camera, scene;
			var canvasRenderer, webglRenderer;

			var mesh;
			var texture, texture1, texture2, texture3;

			init();
			animate();

			function init() {

				var width = window.innerWidth / 2 - 2;
				var height = window.innerHeight;

				camera = new THREE.PerspectiveCamera( 70, width / height, 1, 4000 );
				camera.position.z = 2000;

				scene = new THREE.Scene();

				var textureLoader = new THREE.TextureLoader();

				// MESH - Repeat Pattern

				texture = textureLoader.load( 'textures/UV_Grid_Sm.jpg' );
				texture.wrapS = THREE.RepeatWrapping;
				texture.wrapT = THREE.RepeatWrapping;

				var geometry = new THREE.BoxGeometry( 150, 150, 150, 4, 4, 4 );
				var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: 0.5 } );

				mesh = new THREE.Mesh( geometry, material );
				scene.add( mesh );


				// SPRITES - from Sprite Sheet

				texture1 = textureLoader.load( 'textures/UV_Grid_Sm.jpg', function() {

					texture1.wrapS = THREE.ClampToEdgeWrapping;
					texture1.wrapT = THREE.ClampToEdgeWrapping;

					for ( var i = 0; i < 20; i ++ ) {

						var tex = texture1.clone();

						tex.needsUpdate = true; // cloning does not set this

						tex.offset.x = 0.1 * THREE.Math.randInt( 0, 7 );
						tex.offset.y = 0.1 * THREE.Math.randInt( 0, 7 );
						tex.repeat.x = 0.1 * THREE.Math.randInt( 1, 3 );
						tex.repeat.y = 0.1 * THREE.Math.randInt( 1, 3 );

						var material = new THREE.SpriteMaterial( { map: tex } );
						material.rotation = 0.5 * Math.PI * ( Math.random() - 0.5 );

						var sprite = new THREE.Sprite( material );

						sprite.position.x = Math.random() * 1000 - 500;
						sprite.position.y = Math.random() * 1000 - 500;
						sprite.position.z = Math.random() * 1000 - 500;

						sprite.scale.set( 96, 96, 1 );

						scene.add( sprite );

					}

				} );


				// SPRITES - Repeat Pattern

				texture2 = textureLoader.load( 'textures/sprite0.jpg' );
				texture2.wrapS = THREE.RepeatWrapping;
				texture2.wrapT = THREE.RepeatWrapping;

				for ( var i = 0; i < 20; i ++ ) {

					var material = new THREE.SpriteMaterial( { map: texture2, opacity: 0.7 } );
					var sprite = new THREE.Sprite( material );

					sprite.material.rotation = 0.5 * Math.PI * ( Math.random() - 0.5 );

					sprite.position.x = Math.random() * 1000 - 500;
					sprite.position.y = Math.random() * 1000 - 500;
					sprite.position.z = Math.random() * 1000 - 500;

					sprite.scale.set( 64, 64, 1 );

					scene.add( sprite );

				}


				// SPRITES - PNG

				texture3 = textureLoader.load( 'textures/sprite1.png' );
				texture3.wrapS = THREE.ClampToEdgeWrapping;
				texture3.wrapT = THREE.ClampToEdgeWrapping;

				for ( var i = 0; i < 20; i ++ ) {

					var material = new THREE.SpriteMaterial( { map: texture3 } );
					material.rotation = 2 * Math.PI * ( Math.random() - 0.5 );

					var sprite = new THREE.Sprite( material );

					sprite.position.x = Math.random() * 1000 - 500;
					sprite.position.y = Math.random() * 1000 - 500;
					sprite.position.z = Math.random() * 1000 - 500;

					sprite.scale.set( 64, 64, 1 );

					scene.add( sprite );

				}

				//

				canvasRenderer = new THREE.CanvasRenderer();
				canvasRenderer.setClearColor( 0xf0f0f0 );
				canvasRenderer.setPixelRatio( window.devicePixelRatio );
				canvasRenderer.setSize( width, height );
				var container1 = document.getElementById( 'container1' );
				container1.appendChild( canvasRenderer.domElement );

				webglRenderer = new THREE.WebGLRenderer( { antialias: true } );
				webglRenderer.setClearColor( 0xf0f0f0 );
				webglRenderer.setPixelRatio( window.devicePixelRatio );
				webglRenderer.setSize( width, height );
				var container2 = document.getElementById( 'container2' );
				container2.appendChild( webglRenderer.domElement );

				stats = new Stats();
				document.body.appendChild( stats.dom );

				//

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				var width = window.innerWidth / 2 - 2;
				var height = window.innerHeight;

				camera.aspect = width / height;
				camera.updateProjectionMatrix();

				canvasRenderer.setSize( width, height );
				webglRenderer.setSize( width, height );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {

				var time = performance.now() * 0.0001;

				camera.position.x = Math.sin( time ) * 500;
				camera.position.z = Math.cos( time ) * 500;
				camera.lookAt( scene.position );

				//

				texture.repeat.x = Math.sin( time ) * 0.5 + 1.0;
				texture.repeat.y = Math.cos( time ) * 0.5 + 1.0;

				texture.offset.x = Math.sin( time ) * 0.5 + 0.5;
				texture.offset.y = Math.cos( time ) * 0.5 + 0.5;

				mesh.rotation.x = time * 2;
				mesh.rotation.y = time * 4;

				//

				texture2.repeat.x = Math.sin( time ) + 1.25;
				texture2.repeat.y = Math.sin( time ) + 1.25;

				texture2.offset.x = Math.sin( time );
				texture2.offset.y = Math.cos( time );

				//

				canvasRenderer.render( scene, camera );
				webglRenderer.render( scene, camera );

			}

		</script>

	</body>

</html>
